Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Finding the sum of a "counter" variable loop that ran ten times then was pushed into the "numbers" array. Each way I tried resulted with a list

I'm asking for help to find the sum of an array with elements that were pushed from a counter variable that had previously looped 10 times. I'm new to Javascript and was practicing for an assessment, and I've tried several different ways to do it and have only resulted with just a list of the elements within the numbers array.

var counter = 10;
var numbers = [];

for (i = 1; i <= 10; i ++) {
  counter = [i + 73];
  numbers.push(counter);
}

console.log(numbers);

function sum(arr) {
  var s = 0;
  for(var i = 0; i < arr.length; i++) {
     s = s += arr[i];
  }
  return s;
}

console.log(sum([numbers]));

function getArraySum(a) {
  var total = 0;
  for (var i in a) {
    total += a[i];
  }
  return total;
}

var numbers = getArraySum([numbers]);
console.log(numbers);

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you should push only the value of counter without the brackets and then make a reduce to have the sum of each number in the array

var counter = 10;
var numbers = [];

for (i = 1; i <= 10; i++) {
  counter = i + 73;
  numbers.push(counter);
}

console.log(numbers.reduce((a,b) => a+b));

about 4 years ago · Juan Pablo Isaza Relatório

0

You had a couple of typos in the code:

Typos
  1. You were wrapping the sum in square brackets:
counter = [i + 73];

You should just remove the brackets like:

counter = i + 73;

2. You were wrapping a value that is already an array in square brackets while passing it as an argument to a function:
sum( [numbers] )

// ...

getArraySum( [numbers] );

You should remove the brackets, like this:

sum( numbers );

// ...

getArraySum( numbers );

Fix

I updated the code that you shared to fix the above-mentioned things:

var numbers = [];

// Loop 10 times and push each number to the numbers array
for (var i = 1; i <= 10; i ++) {
  var sumNumbers = i + 73;
  numbers.push(sumNumbers);
}


console.log(numbers);


function sum(arr) {
  var total = 0;

  for(var i = 0; i < arr.length; i++) {
     total += arr[i];
  }

  return total;
}

// Call the function by passing it the variable numbers, holding an array
var result1 = sum(numbers);
console.log( result1 );



function getArraySum(a) {
  var total = 0;
  for (var i in a) {
    total += a[i];
  }
  return total;
}

var result2 = getArraySum(numbers);
console.log(result2);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda